<?php

namespace common\logics\shop\bak;

use Yii;
use common\helpers\ArrayHelper;

/**
 * 商品规格信息模型
 * 
 * @author emhome<emhome@163.com>
 * @since 2.0
 */
class SpuSpec_3 extends \common\models\views\ViewSkuSpec {

    /**
     * {@inheritdoc}
     */
    public static function findById($id, $specs) {
        $skus = static::find()->where([
            'product_id' => $id
        ])->all();
        $checkSpecValues = $specs ? explode(',', $specs) : [];
        return static::formatSpecsGroup($skus, $checkSpecValues);
    }

    /**
     * 分组格式化规格属性
     * @param type $data
     * @param type $checkSpecValues
     * @return array
     */
    public static function formatSpecsGroup($data, $checkSpecValues = []) {
        if (empty($data)) {
            return [];
        }
        //所有有效的库存SKU
        list($tempId, $skuSpecs) = static::activeSpecs($data, $checkSpecValues);

        $forceSkuId = 0;
        $forceSku = [];

        //选择的规格对应的所有规格值
        $goodsSpecIds = [];
        $newSpecs = [];
        //检测是否定位到SKU
        foreach ($skuSpecs as $skuid => $skuspec) {
            if (empty(array_diff($skuspec, $checkSpecValues))) {
                $forceSkuId = $skuid;
                $forceSku = $skuspec;
                break;
            }
            if ($checkSpecValues == array_intersect($checkSpecValues, $skuspec)) {
                $goodsSpecIds = ArrayHelper::merge($goodsSpecIds, $skuspec);
                $newSpecs[$skuid] = $skuspec;
            }
        }
        //唯一确定了SKU的情况
        if ($forceSku) {
            list($goodsSpecIds, $newSpecs) = static::composeSpecsByUniqueSku($skuSpecs, $forceSku);
        }
        return static::formatSpecs($data, $checkSpecValues, $forceSkuId, array_keys($newSpecs), $goodsSpecIds, $tempId);
    }

    /**
     * 根据唯一确定的SKU计算各种SKU规格组合的规格SKU
     * @param type $skuSpecs
     * @param type $force
     * @return array
     */
    protected static function composeSpecsByUniqueSku($skuSpecs, $force) {
        $count = count($force);
        $queueSpecs = array_merge($force, $force);
        $specsValues = [];
        $specIds = [];
        for ($i = 0; $i < $count; $i++) {
            $tempCheckSpecValues = array_slice($queueSpecs, $i, $count - 1);
            //检测是否定位到SKU
            foreach ($skuSpecs as $skuid => $skuspec) {
                if ($tempCheckSpecValues == array_intersect($tempCheckSpecValues, $skuspec)) {
                    $specIds = ArrayHelper::merge($specIds, $skuspec);
                    $specsValues[$skuid] = $skuspec;
                }
            }
        }
        return [
            $specIds,
            $specsValues
        ];
    }

    /**
     * 分组格式化规格属性
     * @param type $specs
     * @param type $checkSpecValues
     * @param type $forceSkuId
     * @param type $goodsIds
     * @param type $goodsSpecIds
     * @param type $tempId
     * @return array
     */
    public static function formatSpecs($specs, $checkSpecValues = [], $forceSkuId = 0, $goodsIds = [], $goodsSpecIds = [], $tempId = []) {
        $keys = $goods = $items = [];
        foreach ($specs as $spec) {
            if (empty($goods) && $forceSkuId && $spec->goods_id == $forceSkuId) {
                $goods = $spec;
            }
            $_goods = ArrayHelper::getValue($items, [$spec->spec_id, $spec->spec_value_id, 'goods'], []);
            if ($spec->allowSales > 0) {
                if (in_array($spec->goods_id, $goodsIds)) {
                    $_goods[$spec->goods_id] = 1;
                } elseif (in_array($spec->spec_value_id, $goodsSpecIds)) {
                    $_goods[$spec->goods_id] = 1;
                } elseif (in_array($spec->spec_id, $tempId)) {
                    $_goods[$spec->goods_id] = 1;
                }
            }
            ArrayHelper::setValue($items, [$spec->spec_id, $spec->spec_value_id], [
                'id' => $spec->spec_value_id,
                'name' => $spec->spec_value,
                'checked' => in_array($spec->spec_value_id, $checkSpecValues),
                'clickable' => true,
                'goods' => $_goods,
            ]);
            ArrayHelper::setValue($keys, [$spec->spec_id], [
                'id' => $spec->spec_id,
                'name' => $spec->spec_name,
            ]);
        }

        $data = [];
        foreach ($keys as $kid => $kvls) {
            $_items = ArrayHelper::remove($items, $kid);
            if (empty($_items)) {
                continue;
            }

            $_kvitems = [];
            foreach ($_items as $_iv) {
                $_skus = ArrayHelper::remove($_iv, 'goods', []);
                $_iv['clickable'] = count($_skus) > 0 ? true : false;
                if ($_iv['checked'] && !$_iv['clickable']) {
                    $_iv['checked'] = false;
                }
                $_kvitems[] = $_iv;
            }
            $kvls['items'] = $_kvitems;
            $data[] = $kvls;
        }

        return [
            'checkSpecValues' => $checkSpecValues,
            'goods' => $goods,
            'specs' => $data,
        ];
    }

    /**
     * 所有有效的库存SKU规格信息
     * @param type $specs
     * @param type $checkSpecValues
     * @return array
     */
    protected static function activeSpecs($specs, $checkSpecValues) {
        $specIds = $skuSpecs = [];
        foreach ($specs as $spec) {
            if ($spec->allowSales > 0) {
                $skuSpecs[$spec->goods_id][] = $spec->spec_value_id;
            }
            if (in_array($spec->spec_value_id, $checkSpecValues)) {
                $specIds[$spec->spec_id] = 1;
            }
        }
        return [
            $specIds,
            $skuSpecs,
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id' => 'goods_id',
            'name',
            'product_id',
            'goods_no',
            'thumb',
            'stock' => 'allowSales',
            'price',
            'discount',
            'cost_price',
            'market_price',
        ];
    }

    /**
     * 分组格式化规格属性
     * return array
     */
    public static function formatSpecsGroup2($specs, $checkSpecValues = []) {
        if (empty($specs)) {
            return [];
        }
        $keys = $items = [];
        $goods = [];

        //所有有效的库存SKU
        list($tempId, $skuSpecs) = static::activeSpecs($specs, $checkSpecValues);

        $forceSku = [];
        $forceSkuId = 0;

        //选择的规格对应的所有规格值
        $goodsSpecIds = [];
        $newSpecs = [];
        //检测是否定位到SKU
        foreach ($skuSpecs as $skuid => $skuspec) {
            if (empty(array_diff($skuspec, $checkSpecValues))) {
                $forceSkuId = $skuid;
                $forceSku = $skuspec;
                break;
            }
            if ($checkSpecValues == array_intersect($checkSpecValues, $skuspec)) {
                $goodsSpecIds = ArrayHelper::merge($goodsSpecIds, $skuspec);
                $newSpecs[$skuid] = $skuspec;
            }
        }

        //唯一确定了SKU的情况
        if ($forceSku) {
            $count = count($forceSku);
            $queueSpecs = array_merge($forceSku, $forceSku);
            $composeSpecs = [];
            $goodsSpecIds = [];
            for ($i = 0; $i < $count; $i++) {
                $tempCheckSpecValues = array_slice($queueSpecs, $i, $count - 1);
                //检测是否定位到SKU
                foreach ($skuSpecs as $skuid => $skuspec) {
                    if ($tempCheckSpecValues == array_intersect($tempCheckSpecValues, $skuspec)) {
                        $goodsSpecIds = ArrayHelper::merge($goodsSpecIds, $skuspec);
                        $composeSpecs[$skuid] = $skuspec;
                    }
                }
            }
            $newSpecs = $composeSpecs;
        }
        $goodsIds = array_keys($newSpecs);

        foreach ($specs as $spec) {
            if (empty($goods) && $forceSkuId && $spec->goods_id == $forceSkuId) {
                $goods = $spec;
            }
            $_goods = ArrayHelper::getValue($items, [$spec->spec_id, $spec->spec_value_id, 'goods'], []);
            if ($spec->allowSales > 0) {
                if (in_array($spec->goods_id, $goodsIds)) {
                    $_goods[$spec->goods_id] = 1;
                } elseif (in_array($spec->spec_value_id, $goodsSpecIds)) {
                    $_goods[$spec->goods_id] = 1;
                } elseif (in_array($spec->spec_id, $tempId)) {
                    $_goods[$spec->goods_id] = 1;
                }
            }
            ArrayHelper::setValue($items, [$spec->spec_id, $spec->spec_value_id], [
                'id' => $spec->spec_value_id,
                'name' => $spec->spec_value,
                'checked' => in_array($spec->spec_value_id, $checkSpecValues),
                'clickable' => true,
                'goods' => $_goods,
            ]);
            ArrayHelper::setValue($keys, [$spec->spec_id], [
                'id' => $spec->spec_id,
                'name' => $spec->spec_name,
            ]);
        }

        $data = [];
        foreach ($keys as $kid => $kvls) {
            $_items = ArrayHelper::remove($items, $kid);
            if (empty($_items)) {
                continue;
            }

            $_kvitems = [];
            foreach ($_items as $_iv) {
                $_skus = ArrayHelper::remove($_iv, 'goods', []);
                $_iv['clickable'] = count($_skus) > 0 ? true : false;
                if ($_iv['checked'] && !$_iv['clickable']) {
                    $_iv['checked'] = false;
                }
                $_kvitems[] = $_iv;
            }
            $kvls['items'] = $_kvitems;
            $data[] = $kvls;
        }

        return [
            'checkSpecValues' => $checkSpecValues,
            'goods' => $goods,
            'specs' => $data,
        ];
    }

}
